﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using System.Collections;
using PlanningGIS.Framework;


using PlanningGIS.Util.Convertor;
using PlanningGIS.Framework.Rule;

namespace PlanningGIS.DataConvert.model
{
    /// <summary>
    ///作用：构造数据转换的超类
    /// </summary>
    public abstract class DataConstruct 
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DataConstruct()
        {
            NeedDelete = false;
            IsCreateTargetFeatCls = false;
            FillGuid2 = false;
        }

        /// <summary>
        ///  通过委托返回进度条当前值
        /// </summary>
        public SetProcessBarCurrent SetProcessBarCurrent { get; set; }

        /// <summary>
        /// 通过委托返回进度条最大值
        /// </summary>
        public SetProcessBarMax SetProcessBarMax { get; set; }

        /// <summary>
        /// 如果遇到目标要素类存在，但是不在目标数据集内，是否需要删除原有要素类然后重新创建
        /// </summary>
        protected bool yesOrNo; 
        
        /// <summary>
        /// 对于以上情况，是否用同一种方式处理
        /// </summary>
        protected bool sameWay = false;   

        /// <summary>
        /// 要入库的源数据集合
        /// </summary>
        public List<IFeatureClass> FeatClsListSource { get; set; }

        /// <summary>
        /// 需要入到的目标数据集合
        /// </summary>
        public List<IFeatureClass> FeatClsListTarget { get; set; }

        /// <summary>
        /// 创建库体用的入库基本规则
        /// </summary>
        public DataImportRuleBase DataImportRuleBase { get; set; }

        /// <summary>
        /// 是否需要删除入库成功后的源数据
        /// </summary>
        public bool NeedDelete { get; set; }

        /// <summary>
        /// 是否强行创建目标要素类
        /// </summary>
        public bool IsCreateTargetFeatCls { get; set; }

        /// <summary>
        /// 目标工作空间，可以由外面传，也可以根据根据实际情况进行构建
        /// </summary>
        public IWorkspace TargetWs { get; set; }

        /// <summary>
        /// 数据转换列表，该类就是为了得到这个这个列表
        /// </summary>
        public List<DlgDataConvert> DlgDataConverts { get; protected set; }

        /// <summary>
        /// 是否创建GlobalID2，并用GlobalID填充（快速更新用）
        /// </summary>
        public bool FillGuid2 { get; set; }

        /// <summary>
        /// 获取字段的对应关系
        /// </summary>
        /// <param name="dlgTargetFeatureClassBase"></param>
        /// <returns></returns>
        protected Hashtable GetFieldRelation(DlgTargetFeatureClassBase dlgTargetFeatureClassBase)
        {
            if (dlgTargetFeatureClassBase == null) return null;
            Hashtable ht = new Hashtable();
            foreach (AttrField fd in dlgTargetFeatureClassBase.attrFields)
            {
                ht.Add(fd.TargetFieldName.Trim(), fd.SourceFieldName.Trim());
            }
            return ht;
        }

        /// <summary>
        /// 获取字段的对应关系
        /// </summary>
        /// <param name="dlgTargetFeatureClassBase"></param>
        /// <returns></returns>
        protected Hashtable GetFieldRelation(List<AttrField> attrFields)
        {
            if (attrFields == null || attrFields.Count == 0) return null;
            Hashtable ht = new Hashtable();
            foreach (AttrField fd in attrFields)
            {
                if (string.IsNullOrWhiteSpace(fd.TargetFieldName)) continue;
                if (string.IsNullOrWhiteSpace(fd.SourceFieldName))
                {
                    ht.Add(fd.TargetFieldName.Trim(), fd.TargetFieldName.Trim());
                }
                else if (fd.SourceFieldName.ToLower() != "<null>")
                {
                    ht.Add(fd.TargetFieldName.Trim(), fd.SourceFieldName.Trim());
                }
            }
            return ht;
        }

        /// <summary>
        /// 获取字段默认值
        /// </summary>
        /// <param name="dlgTargetFeatureClassBase"></param>
        /// <returns></returns>
        protected Hashtable GetDefaultValue(DlgTargetFeatureClassBase dlgTargetFeatureClassBase)
        {
            if (dlgTargetFeatureClassBase == null) return null;
            Hashtable ht = new Hashtable();
            foreach (AttrField fd in dlgTargetFeatureClassBase.attrFields)
            {
                ht.Add(fd.TargetFieldName.Trim(), fd.DefaultValue);
            }
            return ht;
        }

        /// <summary>
        /// 获取字段默认值
        /// </summary>
        /// <param name="dlgTargetFeatureClassBase"></param>
        /// <returns></returns>
        protected Hashtable GetDefaultValue(List<AttrField> attrFields)
        {
            if (attrFields == null || attrFields.Count == 0) return null;
            Hashtable ht = new Hashtable();
            foreach (AttrField fd in attrFields)
            {
                ht.Add(fd.TargetFieldName.Trim(), fd.DefaultValue);
            }
            return ht;
        }

    }
}
